package com.lengyue.imakiba.base;

import java.util.Locale;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.i18n.I18N;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.FreeMarkerRender;
import com.lengyue.imakiba.data.Anime;
import com.lengyue.imakiba.data.AnimeCV;
import com.lengyue.imakiba.data.AnimeType;
import com.lengyue.imakiba.data.CV;
import com.lengyue.imakiba.data.Dict;
import com.lengyue.imakiba.data.Episode;
import com.lengyue.imakiba.data.Type;
import com.lengyue.imakiba.filter.PermissionFilter;
import com.lengyue.imakiba.manage.AnimeCVManage;
import com.lengyue.imakiba.manage.AnimeManage;
import com.lengyue.imakiba.manage.CVManage;
import com.lengyue.imakiba.manage.EpisodeManage;
import com.lengyue.imakiba.manage.TypeManage;
import com.lengyue.imakiba.show.Adv;
import com.lengyue.imakiba.show.AnimePlayer;
import com.lengyue.imakiba.show.Index;
import com.lengyue.imakiba.show.Login;
import com.lengyue.imakiba.show.AnimeInfo;
import com.lengyue.imakiba.show.Search;

import freemarker.template.TemplateModelException;

/**
 * API引导式配置
 */
public class ImakibaConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("config.txt");
		me.setDevMode(getPropertyToBoolean("devMode", false));
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/index",Index.class);
		me.add("/login",Login.class);
		me.add("/animeManage",AnimeManage.class);
		me.add("/typeManage",TypeManage.class);
		me.add("/episodeManage",EpisodeManage.class);
		me.add("/animeInfo", AnimeInfo.class);
		me.add("/cvManage",CVManage.class);
		me.add("/animePlayer",AnimePlayer.class);
		me.add("/Adv",Adv.class);
		me.add("/animeCVManage",AnimeCVManage.class);
		me.add("/search",Search.class);
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		arp.addMapping("anime", Anime.class);
		arp.addMapping("episode", Episode.class);
		arp.addMapping("type", Type.class);
		arp.addMapping("anime_type", AnimeType.class);
		arp.addMapping("dict", Dict.class);
		arp.addMapping("cv", CV.class);
		arp.addMapping("anime_cv", AnimeCV.class);
		arp.setShowSql(true);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new PermissionFilter());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler());
		//me.add(new I18nHandle());
	}

	@Override
	public void afterJFinalStart() {
		try {
			I18N.init("base", Locale.getDefault(), null);
			FreeMarkerRender.getConfiguration().setSharedVariable("i18n", I18N.me());
		} catch (TemplateModelException e) {
			e.printStackTrace();
		}
	}
}
